package server;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

import common.Clock;

public class ClockServer extends AbsServer {
	Clock clock = new Clock();
	
	public ClockServer(int port) {
		super(port);
	}

	public boolean start(){
		if(!super.start()) return false;
		clock.startClock();
		return true;
	}
	
	public void setTime(String tm){
		String[] args = tm.split(":");
		if(args.length >= 2){
			clock.setHour(Integer.parseInt(args[0]));
			clock.setMinute(Integer.parseInt(args[1]));
		}
	}
	public String getTimeString(){
		return clock.getTimeString();
	}
	@Override
	public void replyAction(DatagramSocket socket, DatagramPacket pkt)
			throws Exception {
		pkt.setData(clock.getTimeString().getBytes());
		socket.send(pkt);
	}

}
